package com.sservice.web.controller.admin;

import com.sservice.common.SameUrlData;
import com.sservice.model.dao.MsdsMapper;
import com.sservice.model.vo.MsdsVo;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.model.entity.Msds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/msds")
public class MsdsController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsdsMapper msdsMapper;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @SameUrlData
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Integer> create(Msds arg, HttpSession session) {
        int service_res = msdsMapper.insertSelective(arg);
        BaseResult<Integer> res = new BaseResult<Integer>(true, service_res);
        return res;
    }

    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Msds> get(Msds arg, HttpSession session) {
        Msds service_res = msdsMapper.selectByPrimaryKey(arg.getId());
        BaseResult<Msds> res = new BaseResult<Msds>(true, service_res);
        return res;
    }

    /**
     * @param arg
     * @return
     */
    @ResponseBody
    @SameUrlData
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Object> delete(Msds arg) {
        BaseResult<Object> res = null;
        int service_res = msdsMapper.deleteByPrimaryKey(arg.getId());
        res = new BaseResult(true, service_res);
        return res;
    }

    @ResponseBody
    @SameUrlData
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Object> update(Msds arg) {
        BaseResult<Object> res = null;
        int service_res = msdsMapper.updateByPrimaryKeySelective(arg);
        res = new BaseResult(true, service_res);
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(MsdsVo arg) {
        List<Msds> list = msdsMapper.queryByPage(arg);
        int total = msdsMapper.queryCount(arg);
        ListResult res = new ListResult(true, list);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        res.setPage(page);
        return res;
    }
}
